package app.models.member;

import app.models.DaoKit;
import com.github.sog.annotation.TableBind;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-23 18:17
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_member_currency_record")
public class MemberCurrencyRecord extends Model<MemberCurrencyRecord> {

    public static final MemberCurrencyRecord dao = new MemberCurrencyRecord();

    public List<MemberCurrencyRecord> findByMember(long mid) {
        return find(SqlKit.sql("membercurrencyrecord.findByMember"), mid);
    }

    public Page<Record> datatables(DatatablesCriterias criterias, String date, Member member) {
        Preconditions.checkNotNull(member);
        final List<Object> params = Lists.newArrayList();
        params.add(member.get("id"));
        String where = SqlKit.sql("membercurrencyrecord.where");
        if (!Strings.isNullOrEmpty(date)) {
            where += " AND record_time BETWEEN ? AND ?";
            params.add(date + " 00:00:00");
            params.add(date + " 23:59:59");
        }
        return DaoKit.paginate(where, SqlKit.sql("membercurrencyrecord.columns"), criterias, " ORDER BY record_time DESC", params);
        /*int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();
        String where = " FROM ol_member_currency_record record, ol_member member WHERE record.member=member.id ";
        List<Object> params = Lists.newArrayList();
        if (member != null) {
            if (!Strings.isNullOrEmpty(member.getStr("phone"))) {
                where += " AND member.phone like ?";
                params.add(StringPool.PERCENT + member.getStr("phone") + StringPool.PERCENT);
            }
            if (!Strings.isNullOrEmpty(member.getStr("card_no"))) {
                where += " AND member.card_no like ?";
                params.add(StringPool.PERCENT + member.getStr("card_no") + StringPool.PERCENT);
            }
        }

        if (!Strings.isNullOrEmpty(date)) {
            where += " AND record.record_time BETWEEN ? AND ?";
            params.add(date + " 00:00:00");
            params.add(date + " 23:59:59");
        }

        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY record.record_time DESC";
        }

        return paginate(start, pageSize, SqlKit.sql("membercurrencyrecord.columns"), where, params.toArray());*/
    }
}
